package com.cqjtu.service.serviceImpl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.cqjtu.enums.ResultCodeEnum;
import com.cqjtu.mapper.ManifestMapper;
import com.cqjtu.mapper.TeamsMapper;
import com.cqjtu.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cqjtu.mapper.ProjectMapper;
import com.cqjtu.service.IProjectService;

/**
 * 项目信息Service业务层处理
 * 
 * @author kk
 * @date 2024-03-03
 */
@Service
public class ProjectServiceImpl implements IProjectService 
{
    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private ManifestMapper manifestMapper;

    @Autowired
    private TeamsMapper teamsMapper;

    /**
     * 查询项目信息
     * 
     * @param projectId 项目信息主键
     * @return 项目信息
     */
    @Override
    public ProjectDto selectProjectByProjectId(Integer projectId)
    {
        ProjectDto project = projectMapper.selectProjectByProjectId(projectId);
        Manifest manifest = new Manifest();
        manifest.setProjectId(projectId);
        List<Manifest> manifests = manifestMapper.selectManifestList(manifest);
        project.setManifestList(manifests);
        return project;
    }

    /**
     * 查询项目信息列表
     * 
     * @param projectVo 项目信息
     * @return 项目信息
     */
    @Override
    public List<ProjectVO> selectProjectList(ProjectVO projectVo)
    {

        return projectMapper.selectProjectList(projectVo);
    }

    @Override
    public List<ProjectVO> selectProjectList1(ProjectVO projectVo) {
        return projectMapper.selectProjectList1(projectVo);
    }

    /**
     * 新增项目信息
     * 
     * @param project 项目信息
     * @return 结果
     */
    @Override
    public Result insertProject(ProjectDto project)
    {
        List<Manifest> manifestList = project.getManifestList();


        projectMapper.insertProject(project);
        for(Manifest manifest : manifestList){
            manifest.setProjectId(project.getProjectId());
            manifestMapper.insertManifest(manifest);
        }
        return Result.success();
    }

    /**
     * 修改项目信息
     * 
     * @param project 项目信息
     * @return 结果
     */

    /**
     * 批量删除项目信息
     * 
     * @param projectIds 需要删除的项目信息主键
     * @return 结果
     */
    @Override
    public int deleteProjectByProjectIds(Long[] projectIds)
    {
        for(Long projectId : projectIds){
            manifestMapper.deleteManifestByProjectId(projectId);
        }
        return projectMapper.deleteProjectByProjectIds(projectIds);
    }

    /**
     * 删除项目信息信息
     * 
     * @param projectId 项目信息主键
     * @return 结果
     */
    @Override
    public int deleteProjectByProjectId(Long projectId)
    {
        return projectMapper.deleteProjectByProjectId(projectId);
    }

    @Override
    public void deleteManifestByManifestIds(Long[] manifestIds) {
        List<Integer> integerList = new ArrayList<>();
        for(Long manifestId : manifestIds){
            Integer manifestIdObj = manifestId.intValue();
            integerList.add(manifestIdObj);
        }
        manifestMapper.deleteManifestByManifestIds(integerList);
    }

    @Override
    public void updateManifest(Manifest manifest) {
        manifestMapper.updateManifest(manifest);
    }

    @Override
    public void insertManifest(Manifest manifest) {
        if(manifest.getEngineeringQuantityCompleted() == null || manifest.getEngineeringQuantityCompleted() == ""){
            manifest.setEngineeringQuantityCompleted("0");
        }
        if (manifest.getEngineeringQuantityRemaining() == null || manifest.getEngineeringQuantityRemaining() == "") {
            manifest.setEngineeringQuantityRemaining("0");
        }
        String price = manifest.getPrice();
        if(!(price != null && price != "")){
            manifest.setPrice("1200");
        }
        manifestMapper.insertManifest(manifest);
    }

    @Override
    public Result updateProjectByObj(ProjectDto project) {


        projectMapper.updateProject(project);
        return Result.success();
    }



}
